﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VTV.TaskManagementSystem.Entities;

namespace VTV.TaskManagementSystem.DataAccess
{
    public partial class User : IConvertToLinqEntity<User>, ISetting<Entities.User>
    {
        public string ErrorMessage { get; set; }

        public User Convert(object baseEntity)
        {
            User result = null;
            try
            {
                if (baseEntity != null)
                {
                    var entity = (Entities.User)baseEntity;

                    return result = new User
                    {
                        UserID = entity.UserID,
                        RoleID = entity.RoleID,
                        EmployeeID = entity.EmployeeID,
                        UserName = entity.Username,
                        Password = entity.Password,
                        IsActive = entity.IsActive
                    };
                }
                else
                {
                    ErrorMessage = Common.Constant.ErrorMessage.SOURCE_IS_NULL;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
            }

            return result;
        }

        public bool AddNew(Entities.User source)
        {
            try
            {
                var newSource = this.Convert(source);

                using (var data = new DataBaseDataContext())
                {
                    data.Users.InsertOnSubmit(newSource);
                    data.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }

            return true;
        }

        public bool Delete(Entities.User source)
        {
            try
            {
                var newSource = this.Convert(source);

                if (newSource != null)
                {
                    using (var data = new DataBaseDataContext())
                    {
                        data.Users.DeleteOnSubmit(newSource);
                        data.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool Update(Entities.User source)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var user = data.Users.FirstOrDefault(p => p.UserID == source.UserID);
                    if (user != null)
                    {
                        user.RoleID = source.RoleID;
                        user.EmployeeID = source.EmployeeID;
                        user.UserName = source.Username;
                        user.Password = source.Password;
                        user.IsActive = source.IsActive;

                        data.SubmitChanges();

                        return true;
                    }
                    else
                    {
                        ErrorMessage = Common.Constant.SqlErrorMessage.NOT_EXIST_OBJECT_IN_DB;

                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool CheckLogin(string username, string password, out Entities.Login user)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var check = data.Users
                                    .SingleOrDefault(p => p.UserName.Equals(username) && p.Password.Equals(password) && p.IsActive);

                    if (check != null)
                    {
                        user = (from p in data.Users
                                join emp in data.Employees on p.EmployeeID equals emp.EmployeeID into joinedPEmp
                                from emp in joinedPEmp.DefaultIfEmpty()
                                join rol in data.Roles on p.RoleID equals rol.RoleID
                                where p.UserName.Equals(username) && p.Password.Equals(password) && p.IsActive
                                select new Entities.Login
                                           {
                                               EmployeeID = p.EmployeeID,
                                               EmployeeName = emp.FullName,
                                               UserName = p.UserName,
                                               UserID = p.UserID,
                                               RoleLevel = rol.RoleLevel,
                                               RoleName = rol.Name
                                           }).Single();

                        return true;
                    }
                    else
                    {
                        user = null;
                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
                user = null;
                return false;
            }
        }
    }
}
